`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2020/12/15 10:46:44
// Design Name: 
// Module Name: logic_pic
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module logic_pic
#(
    parameter RGB_DATA_WIDTH = 24,
    parameter MCU_DATA_WIDTH = 16,
    parameter PORCH_DATA_WIDTH = 16,
    parameter FMC_CMD_WIDTH =8,
    parameter BMP_WIDTH = 8
)
(
    input                           lcd_clk,
    input                           rst,
    input                           Display_Mode,
    input  [FMC_CMD_WIDTH-1:0]      Dis_Mode,
    input  [PORCH_DATA_WIDTH-1:0]   h_active,  //H_P
    input  [PORCH_DATA_WIDTH-1:0]   v_active,  //V_P
    input  [PORCH_DATA_WIDTH-1:0]   x_cnt, 
    input  [PORCH_DATA_WIDTH-1:0]   y_cnt,
    input  [BMP_WIDTH-1:0]          pic_num,
    output reg[RGB_DATA_WIDTH-1:0] 	rgb_logic,
    output reg[MCU_DATA_WIDTH-1:0] 	mcu_logic,
    output reg                      rs_logic,
    output reg                      wr_logic
);

reg [RGB_DATA_WIDTH-1:0]     h_bar_data;
reg [RGB_DATA_WIDTH-1:0]     v_bar_data;
wire[RGB_DATA_WIDTH-1:0]     red255_data;
wire[RGB_DATA_WIDTH-1:0]     green255_data;
wire[RGB_DATA_WIDTH-1:0]     blue255_data;
wire[RGB_DATA_WIDTH-1:0]     h_grad255_data1;
wire[RGB_DATA_WIDTH-1:0]     v_grad255_data1;
wire[RGB_DATA_WIDTH-1:0]     h_grad255_data2;
wire[RGB_DATA_WIDTH-1:0]     v_grad255_data2;

reg [RGB_DATA_WIDTH-1:0]     h_255_rgbw_data1;
reg [RGB_DATA_WIDTH-1:0]     v_255_rgbw_data1;
reg [RGB_DATA_WIDTH-1:0]     h_255_rgbw_data2;
reg [RGB_DATA_WIDTH-1:0]     v_255_rgbw_data2;
reg [RGB_DATA_WIDTH-1:0]     cheek_data;
reg [RGB_DATA_WIDTH-1:0]     line0_data;
reg [RGB_DATA_WIDTH-1:0]     line1_data;
reg [RGB_DATA_WIDTH-1:0]     char0_data;
reg [RGB_DATA_WIDTH-1:0]     crosstalk0_data;
reg [RGB_DATA_WIDTH-1:0]     crosstalk1_data;
reg [RGB_DATA_WIDTH-1:0]     crosstalk2_data;
reg [RGB_DATA_WIDTH-1:0]     crosstalk3_data;
reg [RGB_DATA_WIDTH-1:0]     persent15_data;
reg [RGB_DATA_WIDTH-1:0]     grid_data_0;
reg [RGB_DATA_WIDTH-1:0]     grid_data_1;
reg [RGB_DATA_WIDTH-1:0]     grid_data_2;
reg [RGB_DATA_WIDTH-1:0]     grid_data_3;
reg [RGB_DATA_WIDTH-1:0]     grid_data_4;
reg [RGB_DATA_WIDTH-1:0]     rgb_logic_r;
reg [RGB_DATA_WIDTH-1:0]     color_data;
reg [RGB_DATA_WIDTH-1:0]     color_data1;
reg [RGB_DATA_WIDTH-1:0]     color_data2;
reg [RGB_DATA_WIDTH-1:0]     color_data3;
//----------------------------------------------------------------
////////// 彩条图像
//----------------------------------------------------------------
always @(posedge lcd_clk)   
begin
    if (x_cnt==h_active/8)            
            h_bar_data <= 24'hff0000;               //红色
    else if (x_cnt==h_active*2/8)
            h_bar_data <= 24'h00ff00;               //红色                 
    else if (x_cnt==h_active*3/8)            
            h_bar_data <= 24'h0000ff;               //蓝色
    else if (x_cnt==h_active*4/8)            
            h_bar_data <= 24'hff00ff;               //紫色
    else if (x_cnt==h_active*5/8)            
            h_bar_data <= 24'hffff00;               //黄色
    else if (x_cnt==h_active*6/8)            
            h_bar_data <= 24'h00ffff;               //青色
    else if (x_cnt==h_active*7/8)            
            h_bar_data <= 24'hffffff;               //黑色
    else if (x_cnt==h_active)            
            h_bar_data <= 24'h000000;               //白色
    else          
            h_bar_data <= h_bar_data;
end

 always @(posedge lcd_clk)   
  begin
     if (y_cnt==v_active/8)            
               v_bar_data <= 24'hff0000;               //红色
     else if (y_cnt==v_active*2/8)
               v_bar_data <= 24'h00ff00;               //红色                 
     else if (y_cnt==v_active*3/8)            
               v_bar_data <= 24'h0000ff;               //蓝色
     else if (y_cnt==v_active*4/8)            
               v_bar_data <= 24'hff00ff;               //紫色
     else if (y_cnt==v_active*5/8)            
               v_bar_data <= 24'hffff00;               //黄色
     else if (y_cnt==v_active*6/8)            
               v_bar_data <= 24'h00ffff;               //青色
     else if (y_cnt==v_active*7/8)            
               v_bar_data <= 24'hffffff;               //黑色
     else if (y_cnt==v_active)            
               v_bar_data <= 24'h000000;               //白色
     else          
               v_bar_data <= v_bar_data;
   end
       
//----------------------------------------------------------------
////////// 横向渐变图像，需在1个clk周期内计算出灰阶值，不可直接使用除法
//----------------------------------------------------------------     
reg[18:0] H_0_511;
wire[18:0] Increase_H;
assign  Increase_H[18:0] = (19'd523264/h_active);
always @(posedge lcd_clk or posedge rst)  begin
    if(rst)
        H_0_511<=19'h7ffff;
    else if(x_cnt<h_active)
        H_0_511<=H_0_511+Increase_H;
    else
        H_0_511<=19'h7ffff;    
end
assign  h_grad255_data1 ={3{8'hff-H_0_511[18:11]}}; 
assign  h_grad255_data2 ={3{H_0_511[18:11]}};  

always @(posedge lcd_clk)  begin
    if(y_cnt>=1'd0 & y_cnt<v_active*1/4)        begin
        h_255_rgbw_data1<={8'hff-H_0_511[18:11],8'h00,8'h00};    
        h_255_rgbw_data2<={H_0_511[18:11],8'h00,8'h00}; 
    end
    else if(y_cnt>=v_active*1/4 & y_cnt < v_active*2/4)   begin  
        h_255_rgbw_data1<={8'h00,H_0_511[18:11],8'h00}; 
        h_255_rgbw_data2<={8'h00,8'hff-H_0_511[18:11],8'h00};  
    end
    else if(y_cnt>=v_active*2/4 & y_cnt < v_active*3/4 )   begin
        h_255_rgbw_data1<={8'h00,8'h00,8'hff-H_0_511[18:11]}; 
        h_255_rgbw_data2<={8'h00,8'h00,H_0_511[18:11]}; 
    end
    else if(y_cnt>=v_active*3/4)  begin
        h_255_rgbw_data1<={3{H_0_511[18:11]}};  
        h_255_rgbw_data2<={3{8'hff-H_0_511[18:11]}};  
    end    
end  

//----------------------------------------------------------------
////////// 竖直渐变图像
//---------------------------------------------------------------- 
wire[19:0] V_0_511; //纵向
assign V_0_511 = (y_cnt<<8'd8)/(v_active+1'd1);  
assign v_grad255_data1 ={3{V_0_511[7:0]}}; //0->255
assign v_grad255_data2 ={3{8'hff-V_0_511[7:0]}};    //V_0_511[7:0]  
assign red255_data  = {V_0_511[7:0],8'h00,8'h00};
assign green255_data = {8'h00,V_0_511[7:0],8'h00};
assign blue255_data = {8'h00,8'h00,V_0_511[7:0]};

reg[20:0] V_0_512;   
always @(posedge lcd_clk)  begin
    V_0_512 <= (y_cnt<<8'd8)/(v_active+1'd1); 
    if(x_cnt==1'b0) begin
        v_255_rgbw_data1<={V_0_512[18:11],8'h00,8'h00}; 
        v_255_rgbw_data2<={8'hff-V_0_512[18:11],8'h00,8'h00};   
    end 
    else if(x_cnt==h_active/4)     begin
        v_255_rgbw_data1<={8'h00,V_0_512[18:11],8'h00}; 
        v_255_rgbw_data2<={8'h00,8'hff-V_0_512[18:11],8'h00}; 
    end
    else if(x_cnt==h_active*2/4)   begin
        v_255_rgbw_data1<={8'h00,8'h00,V_0_512[18:11]}; 
        v_255_rgbw_data2<={8'h00,8'h00,8'hff-V_0_512[18:11]}; 
    end
    else if(x_cnt==h_active*3/4)   begin
        v_255_rgbw_data1<={3{V_0_512[18:11]}}; 
        v_255_rgbw_data2<={3{8'hff-V_0_512[18:11]}}; 
    end
end        

//----------------------------------------------------------------
////////// 全色图
//---------------------------------------------------------------- 
wire[20:0] R_0_511;
wire[20:0] L_0_511;
wire[24:0] H_0_512;
assign H_0_512[24:0] = y_cnt*h_active/(v_active+2'd1);  
assign R_0_511[20:0] = (H_0_511[18:11]*V_0_511[7:0])>>4'd8;
assign L_0_511[20:0] = ((8'hff-H_0_511[18:11])*V_0_511[7:0])>>4'd8;

always @(posedge lcd_clk)  begin
    if(x_cnt<H_0_512[15:0])
        color_data2 <= {R_0_511[7:0],V_0_511[7:0],H_0_511[18:11]}; //L
    else
        color_data2 <= {H_0_511[18:11],V_0_511[7:0],R_0_511[7:0]}; //R
        
    if(x_cnt<h_active-H_0_512[15:0])
        color_data3 <= {H_0_511[18:11],V_0_511[7:0],L_0_511[7:0]}; //L
    else
        color_data3 <= {H_0_511[18:11],V_0_511[7:0],R_0_511[7:0]}; //R
        
     color_data <= {8'hff-V_0_511[7:0],V_0_511[7:0],R_0_511[7:0]};
     color_data1 <={8'hff-V_0_511[7:0],V_0_511[7:0],8'hff-R_0_511[7:0]};
end

//----------------------------------------------------------------
////////// 黑底白边框
//---------------------------------------------------------------- 
always @(posedge lcd_clk)  begin
     if(y_cnt>2'd1 & y_cnt<v_active & x_cnt>2'd2 & x_cnt<h_active+1'b1)
        cheek_data<={3{8'h00}};
     else
        cheek_data<={3{8'hff}};
end
    
always @(posedge lcd_clk)  begin
     if(y_cnt==v_active*3/5 | x_cnt==h_active*3/5)
        line0_data<={3{8'hff}};
     else
        line0_data<={3{8'h00}};
end    

//----------------------------------------------------------------
////////////圆上的点距离圆心的距离,半径rr
//---------------------------------------------------------------- 
integer rx,ry,rr;
always@(posedge lcd_clk)    begin
        rr=h_active/2'd2;
    end
always@(posedge lcd_clk)    begin
        rx=x_cnt-h_active/2'd2;
        ry=y_cnt-v_active/2'd2;
    end
always @(posedge lcd_clk) begin
        if(rx*rx+ry*ry<=rr*rr) line1_data<={3{8'hff}};
        else line1_data<={3{8'h00}};   
    end
//----------------------------------------------------------------
////////// F图像
//---------------------------------------------------------------- 
always @(posedge lcd_clk)  begin
     if(y_cnt>v_active*1/8 & y_cnt<v_active*7/8 & x_cnt>h_active*1/6 & x_cnt< h_active*2/6)
        char0_data<={3{8'h00}};
     else if(y_cnt>v_active*1/8 & y_cnt<v_active*2/8 & x_cnt>=h_active*2/6 & x_cnt< h_active*5/6)
        char0_data<={3{8'h00}};
     else if(y_cnt>v_active*4/8 & y_cnt<v_active*5/8 & x_cnt>=h_active*2/6 & x_cnt< h_active*5/6)
        char0_data<={3{8'h00}};     
     else
        char0_data<={3{8'h80}};
end
//----------------------------------------------------------------
////////// Crosstalk图像
//---------------------------------------------------------------- 
always @(posedge lcd_clk)  begin
     if((y_cnt<v_active/4 | y_cnt>v_active*3/4) & (x_cnt<h_active/4 | x_cnt>h_active*3/4))
        crosstalk0_data<={3{8'h00}};
     else
        crosstalk0_data<={3{8'h80}};
end

always @(posedge lcd_clk)  begin
     if((y_cnt<v_active/4 | y_cnt>v_active*3/4) & x_cnt>h_active/4 & x_cnt<h_active*3/4)
        crosstalk1_data<={3{8'h00}};
     else
        crosstalk1_data<={3{8'h80}};
end

always @(posedge lcd_clk)  begin
     if(y_cnt>v_active/4 & y_cnt<v_active*3/4  & (x_cnt<h_active/4 | x_cnt>h_active*3/4))
        crosstalk2_data<={3{8'h00}};
     else
        crosstalk2_data<={3{8'h80}};
end

always @(posedge lcd_clk)  begin
     if(y_cnt>v_active/4 & y_cnt<v_active*3/4 & x_cnt>h_active/4 & x_cnt<h_active*3/4)
        crosstalk3_data<={3{8'h00}};
     else
        crosstalk3_data<={3{8'h80}};
end

always @(posedge lcd_clk)  begin
     if(y_cnt>v_active*3/9 & y_cnt<v_active*6/9 & x_cnt>h_active/4 & x_cnt<h_active*3/4)
        persent15_data<={3{8'hff}};
     else
        persent15_data<={3{8'h00}};
end
//----------------------------------------------------------------
////////// 棋盘格图像
//---------------------------------------------------------------- 
always @(posedge lcd_clk)   begin
    if ((x_cnt[1]==1'b1) ^ (y_cnt[1]==1'b1))            //产生格子0图像
          grid_data_0<={3{8'h00}};
    else
          grid_data_0<= {3{8'hff}};
          
    if ((x_cnt[4]==1'b1) ^ (y_cnt[4]==1'b1))            //产生格子1图像
          grid_data_1<={3{8'h00}};
    else
          grid_data_1<= {3{8'hff}};
                
    if ((x_cnt[6]==1'b1) ^ (y_cnt[6]==1'b1))            //产生格子2图像 
          grid_data_2<={3{8'h00}};
    else
          grid_data_2<={3{8'hff}}; 
          
    if ((x_cnt[7]==1'b1) ^ (y_cnt[7]==1'b1))            //产生格子3图像 
          grid_data_3<={3{8'h00}};
    else
          grid_data_3<={3{8'hff}};      
        
    if ((x_cnt[8]==1'b1) ^ (y_cnt[8]==1'b1))            //产生格子3图像 
          grid_data_4<={3{8'h00}};
    else
          grid_data_4<={3{8'hff}};   
end

  
always @(posedge lcd_clk) begin
    case(pic_num)
        8'h00: begin
             rgb_logic <= h_bar_data;
        end
        8'h01: begin
             rgb_logic <= v_bar_data;
        end
        8'h02: begin
             rgb_logic <= h_grad255_data1;
        end
        8'h03: begin
             rgb_logic <= h_grad255_data2;
        end   
        8'h04: begin
             rgb_logic <= v_grad255_data1;
        end
        8'h05: begin
             rgb_logic <= v_grad255_data2;
        end   
        8'h06: begin
             rgb_logic <= h_255_rgbw_data1;
        end  
        8'h07: begin
             rgb_logic <= h_255_rgbw_data2;
        end     
        8'h08: begin
             rgb_logic <= v_255_rgbw_data1;
        end  
        8'h09: begin
             rgb_logic <= v_255_rgbw_data2;
        end     
        8'h0a: begin
             rgb_logic <= cheek_data;
        end   
        8'h0b: begin
             rgb_logic <= crosstalk0_data;
        end    
        8'h0c: begin
             rgb_logic <= crosstalk1_data;
        end    
        8'h0d: begin
             rgb_logic <= crosstalk2_data;
        end    
        8'h0e: begin
             rgb_logic <= crosstalk3_data;
        end
        8'h0f: begin
             rgb_logic <= grid_data_0;
        end  
        8'h10: begin
             rgb_logic <= grid_data_1;
        end  
        8'h11: begin
             rgb_logic <= grid_data_2;
        end     
        8'h12: begin
             rgb_logic <= grid_data_3;
        end    
        8'h13: begin
             rgb_logic <= grid_data_4;
        end  
        8'h14: begin
             rgb_logic <= red255_data;
        end    
        8'h15: begin
             rgb_logic <= green255_data;
        end  
        8'h16: begin
             rgb_logic <= blue255_data;
        end  
        8'h17: begin
             rgb_logic <= char0_data;
        end  
        8'h18: begin
             rgb_logic <= line0_data;
        end    
        8'h19: begin
             rgb_logic <= line1_data;
        end    
        8'h1a: begin
             rgb_logic <= persent15_data; //15% 开窗
        end    
        8'h1b: begin
             rgb_logic <= 24'hffffff;
        end 
        8'h1c: begin
             rgb_logic <= 24'h808080;
        end  
        8'h1d: begin
             rgb_logic <= 24'h404040;
        end    
        8'h1e: begin
             rgb_logic <= 24'h202020;
        end  
        8'h1f: begin
             rgb_logic <= 24'h101010;
        end   
        8'h20: begin
             rgb_logic <= 24'h000000;
        end    
        8'h21: begin
             rgb_logic <= color_data;
        end   
        8'h22: begin
             rgb_logic <= color_data1;
        end  
        8'h23: begin
             rgb_logic <= color_data2;
        end  
        8'h24: begin
             rgb_logic <= color_data3;
        end  
        default:begin
             rgb_logic <= 24'hffffff;
        end
    endcase;
end



endmodule